package com.gab.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gab.mapper.TbCompanyMapper;
import com.gab.pojo.TbCompany;
import com.gab.service.TbCompanyService;
import com.gab.utils.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import  com.gab.pojo.TbCompanyExample;
import  com.gab.pojo.TbCompanyExample.Criteria;

@Service
public class TbCompanyServiceImpl implements TbCompanyService{
	@Autowired
	private TbCompanyMapper tbcompanyMapper;

	public List<TbCompany> findAll() {
		
		return tbcompanyMapper.selectByExample(null);
	}

	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbCompany> page=   (Page<TbCompany>) tbcompanyMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	public void add(TbCompany tbCompany) {
		tbcompanyMapper.insert(tbCompany);
		
	}

	public void update(TbCompany tbCompany) {
		tbcompanyMapper.updateByPrimaryKey(tbCompany);
		
	}

	

	
	public void delete(Long[] ids) {
		for(Long id:ids){
			tbcompanyMapper.deleteByPrimaryKey(id);
		}
	}

	public PageResult findPage(TbCompany tbCompany, int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
		
		TbCompanyExample example=new TbCompanyExample();
		Criteria criteria = example.createCriteria();
		
		if(tbCompany!=null){			
			if(tbCompany.getAddr()!=null && tbCompany.getAddr().length()>0){
				criteria.andAddrLike("%"+tbCompany.getAddr()+"%");
			}
			if(tbCompany.getCode()!=null && tbCompany.getCode().length()>0){
				criteria.andCodeLike("%"+tbCompany.getCode()+"%");
			}
			if(tbCompany.getCopyright()!=null && tbCompany.getCopyright().length()>0){
				criteria.andCopyrightLike("%"+tbCompany.getCopyright()+"%");
			}
			if(tbCompany.getEmail()!=null && tbCompany.getEmail().length()>0){
				criteria.andEmailLike("%"+tbCompany.getEmail()+"%");
			}
			if(tbCompany.getPhone()!=null && tbCompany.getPhone().length()>0){
				criteria.andPhoneLike("%"+tbCompany.getPhone()+"%");
			}
			if(tbCompany.getQq()!=null && tbCompany.getQq().length()>0){
				criteria.andQqLike("%"+tbCompany.getQq()+"%");
			}
			if(tbCompany.getTel()!=null && tbCompany.getTel().length()>0){
				criteria.andTelLike("%"+tbCompany.getTel()+"%");
			}
	
		}
		
		Page<TbCompany> page= (Page<TbCompany>)tbcompanyMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	public TbCompany findOne(Long id) {
		
		return tbcompanyMapper.selectByPrimaryKey(id);
	}

}
